﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using log4net;
using CashFlow.Db.DAL;
using CashFlow.Db.Entity;
using SysAdmin._00_Def;

namespace SysAdmin._03_Service._00_Db
{
    public class OrderService
    {
        public static OrderEntity QueryById(int orderId)
        {
            using (var db = SqlSugarHelper.GetDb())
            {
                var entity = db.Queryable<OrderEntity>().Where(x => x.Id == orderId).First();
                return entity;
            }
        }

        public static IList<OrderBuySellInfo> QueryAllOrders(string date
            , string orderNo
            , string name
            , int state)
        {
            using (var db = SqlSugarHelper.GetDb())
            {
                var query = db.Queryable<OrderEntity>()
                    .InnerJoin<UserInfoEntity>((o, u1) => o.BuyerUserId == u1.Id)
                    .InnerJoin<UserInfoEntity>((o, u1, u2) => o.SellerUserId == u2.Id);

                if (!string.IsNullOrEmpty(date))
                {
                    query = query.Where((o, u1, u2) => o.CreateTime.ToString().Substring(0, 10) == date);
                }
                if (!string.IsNullOrEmpty(orderNo))
                {
                    query = query.Where((o, u1, u2) => o.OrderNo == orderNo);
                }
                if (!string.IsNullOrEmpty(name))
                {
                    query = query.Where((o, u1, u2) => u1.Name.Contains(name) || u2.Name.Contains(name));
                }
                if (state >= 0)
                {
                    query = query.Where((o, u1, u2) => o.State == state);
                }

                var ls = query.Select((o, u1, u2) => new OrderBuySellInfo
                {
                    Order = o,
                    Buyer = u1,
                    Seller = u2
                }).ToList();
                return ls;
            }
        }

        public static IList<OrderEntity> QueryAllOrders2(string date)
        {
            using (var db = SqlSugarHelper.GetDb())
            {
                var ls = db.Queryable<OrderEntity>()
                        .Where(x => x.CreateTime.ToString().Substring(0, 10) == date)
                        .ToList();
                return ls;
            }
        }

        public static bool IsExistOrder(int buyerUserId, string date)
        {
            using (var db = SqlSugarHelper.GetDb())
            {
                int n = db.Queryable<OrderEntity>()
                        .Count(x => x.BuyerUserId == buyerUserId
                            && x.CreateTime.ToString().Substring(0, 10) == date);
                return n > 0;
            }
        }

        public static IList<OrderAndUserInfo> QueryByShopManagerUserId(int shopManagerUserId
            , string date)
        {
            using (var db = SqlSugarHelper.GetDb())
            {
                var ls = db.Queryable<OrderEntity>()
                            .InnerJoin<ShopUserEntity>((o, su) => o.BuyerUserId == su.UserId)
                            .InnerJoin<UserInfoEntity>((o, su, u) => o.BuyerUserId == u.Id)
                            .Where((o, su, u) => su.ShopManagerUserId == shopManagerUserId
                                && o.CreateTime.ToString().Substring(0, 10) == date)
                            .Select((o, su, u) => new OrderAndUserInfo
                            {
                                OrderInfo = o,
                                UserInfo = u
                            })
                            .ToList();
                return ls;
            }
        }

        public static bool Add(OrderEntity order)
        {
            using (var db = SqlSugarHelper.GetDb())
            {
                int n2 = db.Insertable<OrderEntity>(order).ExecuteCommand();
                return n2 > 0;
            }
        }

        public static bool Delete(int orderId)
        {
            using (var db = SqlSugarHelper.GetDb())
            {
                int n = db.Deleteable<OrderEntity>(x => x.Id == orderId).ExecuteCommand();
                return n > 0;
            }
        }
    }
}
